package com.healthcloud.service.accompany.impl;

import cn.hutool.core.collection.CollUtil;
import com.healthcloud.dao.accompany.AccompanyBlDao;
import com.healthcloud.dao.accompany.AccompanyMcDao;
import com.healthcloud.entity.HlDoctorEntity;
import com.healthcloud.entity.accompany.AccompanyBlEntity;
import com.healthcloud.entity.accompany.AccompanyBlVo;
import com.healthcloud.entity.accompany.AccompanyMcEntity;
import com.healthcloud.enums.AccompanyBlHasMedicalFlagEnum;
import com.healthcloud.enums.DelFlagEnum;
import com.healthcloud.service.HlDoctorService;
import com.healthcloud.service.accompany.AccompanyBlService;
import com.healthcloud.service.accompany.AccompanyMcService;
import com.healthcloud.utils.ApiRes;
import com.healthcloud.utils.PageParam;
import com.healthcloud.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service("accompanyMcService")
@Transactional
public class AccompanyMcServiceImpl implements AccompanyMcService {
    @Autowired
    private AccompanyMcDao accompanyMcDao;

    @Autowired
    private HlDoctorService hlDoctorService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public ApiRes saveOrUpdateMc(AccompanyMcEntity entity) {
        /// 获取本医院id和医生id
        Long hospitalId = null;
        Long doctorId = null;
        if (ShiroUtils.getUserId() != 5) {
            HlDoctorEntity hlDoctorEntity = hlDoctorService.queryByUserId(ShiroUtils.getUserId());
            if (hlDoctorEntity == null) {
                return new ApiRes(false, null, "登录账号非医生账号", "登录账号非医生账号");
            }
            hospitalId = hlDoctorEntity.getHospId();
            doctorId = hlDoctorEntity.getId();
        }

        LocalDateTime now = LocalDateTime.now();
        entity.setUpdateTime(now);

        // entity.set
        int i= 0;
        if (null == entity.getId()) {
            /// 新增

            // accompany_bl
            // 本次随访日期
            /*String yyyyMMddStr = DateUtils.toLocalDateTimeStr(entity.getAccompanyTime(), DatePattern.NORM_DATE_PATTERN);
            String accompanyTimeStr = yyyyMMddStr + " 00:00:00";*/

            /// 添加到原来的骨松随访记录表 accompany_bl
            /*Long accompanyBlId = this.addAccompanyBlByExcel(entity, doctorId, accompanyTimeStr);*/

            entity.setHospitalId(hospitalId);
            entity.setDoctorId(doctorId);
            entity.setDeleted(DelFlagEnum.NORMAL.getCode());
            entity.setCreateTime(now);
            i =accompanyMcDao.save(entity);

        } else {
            /// 修改
            // accompany_bl
            // accompany_bl_no_medical
            i =accompanyMcDao.update(entity);
        }
        if (i ==1){
            return ApiRes.success();
        }else {
            return ApiRes.failure();
        }

    }

    @Override
    public List<AccompanyMcEntity> queryList(AccompanyMcEntity entity, PageParam pageParam) {
        return accompanyMcDao.queryList(entity, pageParam);
    }

    @Override
    public Integer queryTotalVo(AccompanyMcEntity entity) {
        return accompanyMcDao.queryTotalVo(entity);
    }

    @Override
    public AccompanyMcEntity queryById(String idStr) {
        return accompanyMcDao.queryById(idStr);
    }

    @Override
    public List<AccompanyMcEntity> medicalServiceById(AccompanyMcEntity entity, PageParam pageParam) {
        return accompanyMcDao.medicalServiceById(entity, pageParam);
    }
}